<?php
/**
 * LoaderManager
 *
 * @category	Loader
 * @package		dayscript.loader
 * @subpackage
 * @author		Nelson Daza <ndaza@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license
 * @version		1.0
 * @version		$Revision: 0 $
 * @filesource
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses
 * @since		1.0
 * @modifiedby	$LastChangedBy: Nelson Daza $
 * @modified	$Date: 2009-08-13 $
 */

namespace dayscript\loader;
/**
 * Requires Loader class definition to work (thinking that it will always have a Dayscript's Framework Loader).
 */
require_once( dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'Loader.class.php' );

/**
 * Requires at least a Dayscript's Framework Loader.
 */
require_once( 'adapter/Dayscript.class.php' );

/**
 * LoaderManager
 *
 * Singleton library autoloader manager.
 *
 * @category	Loader
 * @package		dayscript.loader
 * @subpackage
 * @author		Nelson Daza <ndaza@dayscript.com>
 * @copyright	2009 Dayscript Ltda.
 * @license
 * @version		1.0
 * @link		http://www.dayscript.com
 * @link		{docsLink}
 * @uses		\dayscript\loader\Loader, \dayscript\loader\adapter\Dayscript
 * @since		1.0
 * @modifiedby	$LastChangedBy: Nelson Daza $
 * @modified	$Date: 2009-09-01 $
 */
class LoaderManager	{
	/**
	 * Loaders list
	 * @var Hashtable $loaders
	 * @access protected
	 */
	private static $loaders = array( );
	/**
	 * LoaderManager constructor
	 * @return LoaderManager
	 */
	protected function __construct( )	{
		
	}
	/**
	 * Returns an static intance of LoaderManager
	 * @return LoaderManager
	 */
	public static function init( )	{
		if( empty( self::$loaders ) )	{
			$dayLoader = new \dayscript\loader\adapter\Dayscript( 'dayscript' );
			\spl_autoload_register( __NAMESPACE__ . '\LoaderManager::loadClass' );
		}
	}
	/**
	 * Add a named loader.
	 * This does nothing and returns false if the loader is already registered.
	 * @param \dayscript\loader\Loader $loader
	 */
	public static function addLoader( \dayscript\loader\Loader $loader )	{
		if( isset( self::$loaders[$loader->getName( )] ) )
			return false;
		self::$loaders[$loader->getName( )] = $loader;
		
		return true;
	}
	/**
	 * Method to find a named loader.
	 * @param string $name
	 * @return \dayscript\loader\Loader|null
	 */
	public static function getLoader( $name )	{
		return self::$loaders[$name];
	}
	/**
	 * Get an array of known loader names.
	 * @return array $keys
	 */
	public static function getLoaderNames( )	{
		return \array_keys( self::$loaders );
	}
	/**
	 * Class load function
	 * @param string $className
	 * @return boolean $loaded
	 */
	public static function loadClass( $className )	{
		$className = \trim( $className, '\\' );
		foreach ( self::$loaders as $loader )	{
			$loader->loadClass( $className );
			if( \class_exists( $className ) || \interface_exists( $className ) )
				return true;
		}
		throw new \Exception( 'Class "' . $className . '" was not loaded using: ' . \count( self::$loaders ) . ' loader(s).', \E_USER_ERROR );
	}
}